//
//  CCDetailViewController.m
//  Party
//
//  Created by its on 15/3/26.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCDetailViewController.h"
#import "CCDetailViewModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "CCDebug.h"
#import "CCProgressHUD.h"
@interface CCDetailViewController ()

@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UIImageView *locationImageView;
@property(nonatomic, strong) UILabel *locationLabel;
@property(nonatomic, strong) UILabel *addressLabel;
@property(nonatomic, strong) UIButton *locationButton;
@property(nonatomic, strong) UIImageView *activityImageView;
@property(nonatomic, strong) UILabel *activityLabel;
@property(nonatomic, strong) UILabel *contentLabel;
@property(nonatomic, strong) UIScrollView *imageScrollView;
@property(nonatomic, strong) UIView *imageContainView;
@property(nonatomic, strong) CCDetailViewModel *viewModel;
@property(nonatomic, strong) UIView *containView;
@end

@implementation CCDetailViewController

- (id)initWithViewModel:(id)viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
  }
  return self;
}

- (void)viewDidLoad {
  [self setupUI];
  [self setupNavigationUI];
  [self bindWithViewModel];
  [super viewDidLoad];
}

- (void)loadView {
  UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  view.backgroundColor = [UIColor whiteColor];
  self.view = view;
}

- (void)setupNavigationUI {
  UIBarButtonItem *item1 =
      [[UIBarButtonItem alloc] initWithTitle:@"第一个"
                                       style:UIBarButtonItemStylePlain
                                      target:nil
                                      action:nil];
  item1.enabled = NO;
  item1.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    return [RACSignal empty];
  }];
  UIBarButtonItem *item2 =
      [[UIBarButtonItem alloc] initWithTitle:@"第二个"
                                       style:UIBarButtonItemStylePlain
                                      target:nil
                                      action:nil];
  item2.enabled = NO;
  item2.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    return [RACSignal empty];
  }];
  UIBarButtonItem *item3 =
      [[UIBarButtonItem alloc] initWithTitle:@"第三个"
                                       style:UIBarButtonItemStylePlain
                                      target:nil
                                      action:nil];
  item3.enabled = NO;
  item3.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    return [RACSignal empty];
  }];
  self.navigationItem.rightBarButtonItems = @[ item1, item2, item3 ];
}

- (void)setupUI {
  _scrollView = [UIScrollView new];
  [self.view addSubview:_scrollView];
  [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
  }];
  _containView = [UIView new];
  _containView.backgroundColor = [UIColor lightTextColor];
  [self.scrollView addSubview:_containView];
  UIView *headView = [UIView new];
  headView.backgroundColor = [UIColor whiteColor];
  headView.tag = 1;
  _imageScrollView = [UIScrollView new];
  RANDOM_COLOR(_imageScrollView)
  _titleLabel = [UILabel new];
  RANDOM_COLOR(_titleLabel)[headView addSubview:_titleLabel];
  [headView addSubview:_imageScrollView];
  [self.containView addSubview:headView];
  UIView *addressView = [UIView new];
  addressView.tag = 2;
  addressView.backgroundColor = [UIColor whiteColor];
  _locationImageView = [UIImageView new];
  _locationImageView.image = [UIImage imageNamed:@"g_address.png"];
  RANDOM_COLOR(_locationImageView)
  _addressLabel = [UILabel new];
  _locationLabel = [UILabel new];
  RANDOM_COLOR(_addressLabel) RANDOM_COLOR(_locationButton) _locationButton =
      [UIButton buttonWithType:UIButtonTypeCustom];
  [_locationButton setImage:[UIImage imageNamed:@"g_addressNav.png"]
                   forState:UIControlStateNormal];
  [addressView addSubview:_locationImageView];
  [addressView addSubview:_addressLabel];
  [addressView addSubview:_locationLabel];
  [addressView addSubview:_locationButton];
  [self.containView addSubview:addressView];

  UIView *view = [UIView new];
  view.backgroundColor = [UIColor whiteColor];
  view.tag = 3;
  _activityImageView = [UIImageView new];
  _activityLabel = [UILabel new];
  RANDOM_COLOR(_activityLabel)
  _contentLabel = [UILabel new];
  [view addSubview:_activityImageView];
  [view addSubview:_activityLabel];
  [view addSubview:_contentLabel];
  [self.containView addSubview:view];
  [self viewWithCSS];
}

- (void)viewWithCSS {
  // top
  [_titleLabel sizeToFit];
  [_imageScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(0);
    make.width.mas_equalTo(320);
    make.left.mas_equalTo(0);
    make.height.mas_equalTo(256);
  }];
  [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(10);
    make.top.mas_equalTo(256);
  }];
  UIView *view = [self.containView viewWithTag:1];
  [view mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.containView);
    make.left.mas_equalTo(0);
    make.width.mas_equalTo(DEVICEWIDTH);
    make.bottom.equalTo(_titleLabel.mas_bottom);
  }];

  // address
  UIView *taddressView = [self.containView viewWithTag:2];
  [_locationImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(taddressView).with.offset(10);
    make.top.equalTo(taddressView);
    make.size.mas_equalTo(CGSizeMake(26, 26));
  }];
  [_addressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_locationImageView.mas_right).offset(5);
    make.top.equalTo(taddressView);
    make.right.equalTo(_locationButton.mas_left);
  }];
  [_locationButton mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(taddressView);
    make.size.mas_equalTo(CGSizeMake(60, 75));
    make.top.equalTo(taddressView);
  }];
  [_locationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(
        @[ _addressLabel.mas_bottom, _locationImageView.mas_bottom ]);
    make.left.equalTo(taddressView).with.offset(10);
    make.bottom.equalTo(_locationButton);
    make.right.equalTo(_locationButton.mas_left);
  }];
  RANDOM_COLOR(_locationLabel)
      [taddressView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).with.offset(20);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(DEVICEWIDTH);
        make.bottom.equalTo(_locationButton);
      }];

  // content
  UIView *contentView = [self.containView viewWithTag:3];
  [_activityImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(contentView).with.offset(20);
    make.top.equalTo(contentView);
    make.size.mas_equalTo(CGSizeMake(40, 40));
  }];

  [_activityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_activityImageView.mas_right);
    make.top.equalTo(contentView);
    make.right.equalTo(contentView);
    make.height.mas_equalTo(40);
  }];
  [contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.containView);
    make.top.equalTo(taddressView.mas_bottom).with.offset(20);
    make.width.equalTo(self.containView);
    make.bottom.equalTo(_contentLabel);
  }];
  [self.containView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.scrollView);
    make.left.equalTo(self.scrollView);
    make.width.equalTo(self.scrollView);
    make.bottom.equalTo(contentView);
  }];
}

- (void)bindWithViewModel {
  [RACObserve(self.viewModel, imageArray) subscribeNext:^(NSArray *x) {
    if (x != nil) {
      [self.imageContainView removeFromSuperview];
      self.imageContainView = UIView.new;
      [self.imageScrollView addSubview:self.imageContainView];
      [self.imageContainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.imageScrollView);
        make.height.equalTo(self.imageScrollView);
      }];
      UIView *lastView = nil;
      for (NSString *item in x) {
        UIImageView *imageView = [UIImageView new];
        [self.imageContainView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:item]];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.top.equalTo(@0);
          make.left.equalTo(lastView ? lastView.mas_right : @0);
          make.height.equalTo(self.imageContainView);
          make.width.equalTo(self.imageScrollView);
        }];
        lastView = imageView;
      }
      [self.imageContainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastView.mas_right);
      }];
    }
  } error:^(NSError *error) {

  } completed:^{
  }];

  RAC(self.titleLabel, attributedText) = RACObserve(self.viewModel, title);
  RAC(self.locationLabel, attributedText) =
      RACObserve(self.viewModel, location);
  RAC(self.addressLabel, attributedText) = RACObserve(self.viewModel, address);
  RAC(self.contentLabel, attributedText) = RACObserve(self.viewModel, content);
  RAC(self.activityLabel, attributedText) = RACObserve(self.viewModel, activity);
  
  [[self.viewModel showDetail] subscribeNext:^(id x) {
    [CCProgressHUD showHUBInView:self.view withTimeOut:0 str:@"请稍后..."];
  } error:^(NSError *error) {
    [CCProgressHUD showHUBInView:self.view withTimeOut:2.0f str:@"出错啦..."];
  } completed:^{
    [CCProgressHUD hiddenHUBFromView: self.view];
  }];

  
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
}
@end
